package io.itit.ecp.admin.provide.renderer;

import com.google.common.collect.Maps;
import io.itit.ecp.admin.server.entity.SceneEdificeEntity;
import io.itit.ecp.admin.server.entity.extend.SceneEdificeExtEntity;
import io.itit.ecp.admin.server.service.ISceneEdificeService;
import io.itit.ecp.admin.server.service.ISceneProjectService;
import io.itit.grass.common.server.utils.ConvertUtils;
import io.itit.grass.estelle.starter.parse.AbstractParseRenderer;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：楼栋导入解析器
 * @Date：Create by 2023-11-23 09:55:22
 */
@Scope("prototype")
@Component(SceneEdificeRenderer.ENTITY_NAME)
public class SceneEdificeRenderer extends AbstractParseRenderer<SceneEdificeExtEntity> {

    public static final String ENTITY_NAME = "sceneEdificeRenderer";

    @Resource
    private ISceneEdificeService sceneEdificeService;


    private Set<String> dataSet;

    private Set<String> keySet = new HashSet<>();

    @Resource
    private ISceneProjectService sceneProjectService;

    @Override
    public String entityName() {
        return ENTITY_NAME;
    }

    private Map<String, String> projectMap = Maps.newHashMap();

    @Override
    public Class<SceneEdificeExtEntity> getClazz() {
        return SceneEdificeExtEntity.class;
    }

    @Override
    public int getHeadRowIndex() {
        return 1;
    }

    @Override
    public void processBefore() {
        dataSet = sceneEdificeService.lambdaQuery().list()
                .stream().map(t -> t.getSceneProjectId() + t.getEdificeName())
                .collect(Collectors.toSet());

    }

//    @Override
//    public void verify(SceneEdificeReq sceneEdificeReq) {
//        addErrorMsg(StringUtils.isBlank(sceneEdificeReq.getProjectName()),"项目名称不能为空");
//        String projectId;
//        if (!projectMap.containsKey(sceneEdificeReq.getProjectName())){
//            projectId = sceneProjectService.getProjectIdByName(sceneEdificeReq.getProjectName());
//            if (projectId == null){
//                addErrorMsg("项目名称不存在");
//                return;
//            }
//            projectMap.put(sceneEdificeReq.getProjectName(), projectId);
//        }else {
//            projectId = projectMap.get(sceneEdificeReq.getProjectName());
//        }
//        sceneEdificeReq.setSceneProjectId(projectId);
//
//        addErrorMsg(StringUtils.isBlank(sceneEdificeReq.getEdificeName()),"楼栋名称不能为空");
//        SceneEdificeEntity entity = sceneEdificeService.lambdaQuery()
//                .eq(SceneEdificeEntity::getSceneProjectId, projectId)
//                .eq(SceneEdificeEntity::getEdificeName, sceneEdificeReq.getEdificeName())
//                .one();
//        addErrorMsg(entity != null, "项目下的楼栋名称已存在");
//        if (sceneEdificeReq.getStartNum() == null ||  sceneEdificeReq.getEndNum() == null){
//            addErrorMsg("开始楼层或结束楼层不能为空");
//        }else if (sceneEdificeReq.getStartNum().equals(0) ||  sceneEdificeReq.getEndNum().equals(0)){
//            addErrorMsg("开始楼层或结束楼层不能为0");
//        }else if (sceneEdificeReq.getStartNum() >= sceneEdificeReq.getEndNum()){
//            addErrorMsg("开始楼层需要小于结束楼层");
//        }
//    }

    private String key(String SceneProjectId, String EdificeName) {
        return SceneProjectId + "::" + EdificeName;
    }
    @Override
    public void verify(SceneEdificeExtEntity entity){

        String projectId;

        addErrorMsg(entity.getProjectName() == null,"项目名称不能为空!");
        if (!projectMap.containsKey(entity.getProjectName())){
            projectId = sceneProjectService.getProjectIdByName(entity.getProjectName());
                if (projectId == null){
                addErrorMsg("项目名称不存在！");
                return;
            }
            projectMap.put(entity.getProjectName(), projectId);
        }else {
            projectId = projectMap.get(entity.getProjectName());
        }
        entity.setSceneProjectId(projectId);
        List<SceneEdificeEntity> code = sceneEdificeService.lambdaQuery()
                .eq(SceneEdificeEntity::getSceneProjectId, entity.getSceneProjectId())
                .eq(SceneEdificeEntity::getEdificeName,entity.getEdificeName())
                .list();

        addErrorMsg(entity.getEdificeName() == null,"楼栋名称不能为空!");
        addErrorMsg(entity.getFloorArea() == null,"建筑面积不能为空!");
        addErrorMsg(entity.getFloorArea().doubleValue() <= 0,"建筑面积不能小于等于0!");
        addErrorMsg(entity.getStartNum() == null || entity.getEndNum() == null,"开始或结束楼层不能为空！");
        addErrorMsg(entity.getStartNum() == 0,"开始楼层不能为0！");
        addErrorMsg(entity.getEndNum() == 0,"结束楼层不能为0！");
        addErrorMsg(entity.getEndNum() <= entity.getStartNum(),"开始楼层需要小于结束楼层！");
//        addErrorMsg(entity != null, "项目下的楼栋名称已存在");

        String key = key(entity.getSceneProjectId(), entity.getEdificeName());
        String message = "楼栋名称【" + entity.getEdificeName();

        addErrorMsg(code.size() > 0, message + "】, 数据已存在！");
        addErrorMsg(keySet.contains(key), message + "】, 在导入文件中出现重复！");
        keySet.add(key);

    }
    @Override
    public int processAfter(List<SceneEdificeExtEntity> resultList) {
        if (CollectionUtils.isEmpty(resultList)) {
            return 0;
        }
        List<SceneEdificeEntity> entities = resultList.stream()
                .map(ser->ConvertUtils.copy(ser, SceneEdificeEntity.class))
                .collect(Collectors.toList());
        sceneEdificeService.saveEdificeBatch(entities);
        return resultList.size();
    }
}
